package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Button;

import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.R;

public class CuteTwoButton extends Button {

    /*未选中的图片*/
    private Drawable mCuteOneBackground;

    /*选中后的图片*/
    private Drawable mCuteTwoBackground;

    /*未选中的文字颜色*/
    private int mOneTextColor;

    /*选中后的文字颜色*/
    private int mTwoTextColor;

    private boolean isSelect;

    public CuteTwoButton(Context context) {
        super(context);
        init(context, null);
    }

    public CuteTwoButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CuteTwoButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 设置两个状态的背景资源
     */
    public void setCuteTwoBackground(Drawable cuteOneBackground, Drawable cuteTwoBackground) {
        mCuteOneBackground = cuteOneBackground;
        mCuteTwoBackground = cuteTwoBackground;
        setBackgroundDrawable(mCuteOneBackground);
    }

    /**
     * 设置为状态一
     */
    public void setOneState() {
        if (mCuteOneBackground != null) {
            setTextColor(mOneTextColor);
            setBackgroundDrawable(mCuteOneBackground);
            setBackgroundPadding();
            isSelect = false;
        }
    }

    /**
     * 设置为状态二
     */
    public void setTwoState() {
        if (mCuteTwoBackground != null) {
            setTextColor(mTwoTextColor);
            setBackgroundDrawable(mCuteTwoBackground);
            setBackgroundPadding();
            isSelect = true;
        }
    }

    public boolean isSelect() {
        return isSelect;
    }

    private void setBackgroundPadding() {
        float density = getResources().getDisplayMetrics().density;
        int padL = (int) (4 * density);
        int padR = (int) (4 * density);
        int padY = (int) (0 * density);
        setPadding(padL, padY, padR, padY);
    }

    private void init(Context context, AttributeSet attrs) {
        setTypeface(((ChZhRJApplication) context.getApplicationContext()).getTypeface());
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CuteTwoButton);
        mCuteOneBackground = typedArray.getDrawable(R.styleable.CuteTwoButton_cute_one_background);
        mCuteTwoBackground = typedArray.getDrawable(R.styleable.CuteTwoButton_cute_two_background);
        mOneTextColor = typedArray.getColor(R.styleable.CuteTwoButton_cute_one_text_color, Color.BLACK);
        mTwoTextColor = typedArray.getColor(R.styleable.CuteTwoButton_cute_two_text_color, Color.WHITE);
        typedArray.recycle();
        setOneState();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(null);
        }
    }
}
